package com.mx.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.mx.api.FindService;
import com.mx.service.HelloService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;

/**
 * @author mengbin
 * @date 2018年1月18日 上午11:29:24
 */
@RestController
@Configuration
@DefaultProperties(groupKey="hello-groupKey", commandProperties = {
        @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds", value="2000")
}, threadPoolProperties = {
        @HystrixProperty(name="coreSize", value="2")
})
public class InvokerController {

    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
    
    @Autowired
    private HelloService helloSV;
    
    @Autowired
    private FindService findSV;
    
    @RequestMapping("/hello")
    @HystrixCommand(fallbackMethod="helloFallback", commandKey="hello-commandKey")
    public Object invoke() throws Exception {
        Thread.sleep(3000);
        return helloSV.find();
    }
    
    public String helloFallback() {
        return "hello invoke failed,and fall back";
    }
    
    @RequestMapping("/find")
    public Object invoke2() {
        return findSV.find();
    }
}
